#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class String
{
public:
	String()
    :_pstr(new char[1]())
    {
        cout << "String()" << endl; 
    }

	String(const char * pstr)
    :_pstr(new char[strlen(pstr) + 1]())
    {
        strcpy(_pstr,pstr);
        cout << "String(const char* pstr)" << endl;
    }

    //用另一个字符串初始化
	String(const String & rhs)
    :_pstr(new char[strlen(rhs._pstr) + 1]())
    {
        cout << "String(const String & rhs)" << endl;
        strcpy(_pstr,rhs._pstr);
    }

    ~String(){
        cout << "~String()" << endl;
        if(_pstr){
            delete _pstr;
            _pstr = nullptr;
        }

    }

    String & operator=(const String & rhs){
        cout << "String & operator=(const String & rhs)" << endl;
        if(this != &rhs){
            if(_pstr){
                delete [] _pstr;
            }
            _pstr = new char[strlen(rhs._pstr) + 1]();
            strcpy(_pstr,rhs._pstr);
        }
        return *this;
    }

    String &operator=(const char * pstr){
        cout << "String &operator=(const char * pstr)" << endl;
        if(_pstr){
            delete _pstr;
            _pstr = new char[strlen(pstr) + 1]();
            strcpy(_pstr,pstr);
        }
        return *this;
    }

    String &operator+=(const String & rhs){
        strcat(_pstr,rhs._pstr); 
        return *this;
    }

    String &operator+=(const char * pstr){
        strcat(_pstr,pstr);
        return *this;
    }

    char &operator[](std::size_t index){
        //判断越界
     //   if(index < _capacity -1){
            return _pstr[index];
       /* }else{
            cout << "out of range!" << endl;
            static char nullchar = '\0';
            return nullchar;
        }*/
    }

	const char &operator[](std::size_t index) const;

	std::size_t size() const;
	const char* c_str() const;

	friend bool operator==(const String &, const String &);
	friend bool operator!=(const String &, const String &);

	friend bool operator<(const String &, const String &);
	friend bool operator>(const String &, const String &);
	friend bool operator<=(const String &, const String &);
	friend bool operator>=(const String &, const String &);

	friend std::ostream &operator<<(std::ostream &os, const String &s);
	friend std::istream &operator>>(std::istream &is, String &s);

    void print() {
        if(_pstr){
            cout << _pstr << endl;
        }else{
            cout << endl;
        }
    }

friend
    String operator+(const String & lhs, const String & rhs);
private:
	char * _pstr;
   // size_t _capacity;
};

String operator+(const String & lhs, const String & rhs){
    String des = strcat(lhs._pstr,rhs._pstr);
    return des;
};
String operator+(const String &, const char *);
String operator+(const char *, const String &);

void test0(){
    String str;
    String str0("miaomiao");
    String str1 = str0;

    str0.print();
    str1.print();

    String str2 = "dawang";
    str2.print();

    str1 += str2;
    str1.print();

    str1 += "wansui";
    str1.print();

    String str3 = str1 + str2;

    //cout << str1[0] << endl;
}


int main(int argc, char * argv[])
{
    test0();
    return 0;
}


